package Reminder.view.pages;

import Reminder.dao.ScheduleDao;
import Reminder.dao.impl.ScheduleDaoImpl;
import Reminder.domain.Schedule;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfoenix.controls.JFXButton;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import Reminder.dao.RemindDao;
import Reminder.dao.impl.RemindDaoImpl;
import Reminder.domain.Message;
import Reminder.view.Controller;
import Reminder.view.GlobalConfig;
import Reminder.view.Main;

import java.awt.*;
import java.io.IOException;
import java.text.ParseException;

public class pageOptions extends Page{

    public pageOptions(Stage stage, Controller controller) throws IOException, ParseException, AWTException {
        super(stage, controller);
        if(GlobalConfig.IS_LOGIN){
            RemindDao remindDao = new RemindDaoImpl();
            System.out.println(GlobalConfig.USER_ID);
            Message msg = remindDao.getRemindData(GlobalConfig.USER_ID);
            if(msg.toJSON().getString("status").equals("success")){
                GlobalConfig.ReminderList = JSONArray.parseArray(msg.toJSON().getString("data"));
            }
            ScheduleDao scheduleDao = new ScheduleDaoImpl();
            msg = scheduleDao.getScheduleData(GlobalConfig.USER_ID);
            if(msg.toJSON().getString("status").equals("success")){
                GlobalConfig.Schedule =  Schedule.parseJSON(JSONObject.parseObject(msg.toJSON().getString("data")));
            }
        }
        else{
            RemindDao remindDao = new RemindDaoImpl();
            Message msg = remindDao.getRemindData(GlobalConfig.USER_ID);
            if(msg.toJSON().getString("status").equals("success")){
                GlobalConfig.ReminderList = JSONArray.parseArray(msg.toJSON().getString("data"));
            }
            ScheduleDao scheduleDao = new ScheduleDaoImpl();
            msg = scheduleDao.getScheduleData(GlobalConfig.USER_ID);
            if(msg.toJSON().getString("status").equals("success")){
                GlobalConfig.Schedule =  Schedule.parseJSON(JSONObject.parseObject(msg.toJSON().getString("data")));
            }
        }



        VBox root = new VBox();
        root.setAlignment(Pos.CENTER);
        root.setSpacing(40);

        JFXButton login = new JFXButton("提醒事项");
        login.getStyleClass().add("button");
        login.setPrefSize(250, GlobalConfig.PRIMARY_BUTTON_HEIGHT);
        login.setOnMouseClicked(
                new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent mouseEvent) {
                        GlobalConfig.WINDOW_CURRENT_WIDTH = getStage().getScene().getWidth();
                        GlobalConfig.WINDOW_CURRENT_HEIGHT = getStage().getScene().getHeight();
                        try {
                            pageReminder pageReminder = new pageReminder(getStage(),getController());
                        } catch (IOException | ParseException | AWTException e) {
                            e.printStackTrace();
                        }
                    }
                }
        );
        root.getChildren().add(login);

        JFXButton schedule = new JFXButton("课程表");
        schedule.getStyleClass().add("button");
        schedule.setPrefSize(250, GlobalConfig.PRIMARY_BUTTON_HEIGHT);
//        schedule.setDisable(true);
        schedule.setOnMouseClicked(
                new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent mouseEvent) {
                        GlobalConfig.WINDOW_CURRENT_WIDTH = getStage().getScene().getWidth();
                        GlobalConfig.WINDOW_CURRENT_HEIGHT = getStage().getScene().getHeight();
                        pageSchedule schedule = new pageSchedule(getStage(),getController());
                    }
                }
        );
        root.getChildren().add(schedule);

        JFXButton back = new JFXButton("返回标题");
        back.getStyleClass().add("button_back");
        back.setPrefSize(250, GlobalConfig.PRIMARY_BUTTON_HEIGHT);
        back.setOnMouseClicked(
                new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent mouseEvent) {
                        GlobalConfig.WINDOW_CURRENT_WIDTH = getStage().getScene().getWidth();
                        GlobalConfig.WINDOW_CURRENT_HEIGHT = getStage().getScene().getHeight();
                        pageIndex pageIndex = new pageIndex(getStage(),getController());
                    }
                }
        );
        root.getChildren().add(back);

        Scene scene = new Scene(root, GlobalConfig.WINDOW_CURRENT_WIDTH, GlobalConfig.WINDOW_CURRENT_HEIGHT, Color.WHITE);
        scene.getStylesheets().add(Main.class.getResource("/css/options.css").toExternalForm());
        stage.setScene(scene);

    }
}
